//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class ProductTemplateDBManager
    {
        #region Methods
        public static void DeleteProductTemplate(int ProductTemplateID)
        {
            Database db = NopDataHelper.CreateConnection();
            ProductTemplate productTemplate = GetByProductTemplateID(ProductTemplateID);
            if (productTemplate != null)
            {
                DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductTemplateDelete");
                db.AddInParameter(dbCommand, "ProductTemplateID", DbType.Int32, ProductTemplateID);
                int retValue = db.ExecuteNonQuery(dbCommand);
            }
        }

        public static ProductTemplateCollection GetAllProductTemplates()
        {
            ProductTemplateCollection productTemplateCollection = new ProductTemplateCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductTemplateLoadAll");
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    ProductTemplate productTemplate = new ProductTemplate();
                    productTemplate.ProductTemplateID = NopDataHelper.GetInt(dataReader, "ProductTemplateID");
                    productTemplate.Name = NopDataHelper.GetString(dataReader, "Name");
                    productTemplate.TemplatePath = NopDataHelper.GetString(dataReader, "TemplatePath");
                    productTemplate.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    productTemplate.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    productTemplate.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                    productTemplateCollection.Add(productTemplate);
                }
            }

            return productTemplateCollection;
        }

        public static ProductTemplate GetByProductTemplateID(int ProductTemplateID)
        {

            ProductTemplate productTemplate = null;
            if (ProductTemplateID == 0)
                return productTemplate;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductTemplateLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "ProductTemplateID", DbType.Int32, ProductTemplateID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    productTemplate = new ProductTemplate();
                    productTemplate.ProductTemplateID = NopDataHelper.GetInt(dataReader, "ProductTemplateID");
                    productTemplate.Name = NopDataHelper.GetString(dataReader, "Name");
                    productTemplate.TemplatePath = NopDataHelper.GetString(dataReader, "TemplatePath");
                    productTemplate.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    productTemplate.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    productTemplate.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                }
            }
            return productTemplate;
        }

        public static ProductTemplate InsertProductTemplate(string Name, string TemplatePath,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            ProductTemplate productTemplate = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductTemplateInsert");
            db.AddOutParameter(dbCommand, "ProductTemplateID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "TemplatePath", DbType.String, TemplatePath);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int ProductTemplateID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@ProductTemplateID"));
                productTemplate = GetByProductTemplateID(ProductTemplateID);
            }
            return productTemplate;
        }

        public static ProductTemplate UpdateProductTemplate(int ProductTemplateID, string Name, string TemplatePath,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            ProductTemplate productTemplate = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductTemplateUpdate");
            db.AddInParameter(dbCommand, "ProductTemplateID", DbType.Int32, ProductTemplateID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "TemplatePath", DbType.String, TemplatePath);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                productTemplate = GetByProductTemplateID(ProductTemplateID);


            return productTemplate;
        }
        #endregion
    }
}
